import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LocaleProvider extends ChangeNotifier {
  String? _locale;
  Future<Locale?> getLocale() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _locale = prefs.getString('locale') ?? "system";
    // print("_locale");
    // print(_locale);
    switch (_locale) {
      case 'zhHans':
        return const Locale('zh');
      case 'zhHant':
        return const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant');
      case 'en':
        return const Locale('en');
      default:
        return null;
    }
  }

  changeLanguage(String val) async {
    if (val == _locale) return;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _locale = val;
    await prefs.setString('locale', val);
    notifyListeners();
  }
}
